﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAplicaciones1;

namespace interfaz
{
    public partial class FormGift : Form
    {
        private Sistema sistema;
        private Item item1;
        private Item item2;
        private Item item3;
        private string selected_item;
        private bool cargandoForm=true;

        public FormGift()
        {
            InitializeComponent();
            sistema = Sistema.GetSistema();
            item1 = null;
            item2 = null;
            item3 = null;
            selected_item = "item1";
            cargarDatos();
            cargandoForm = false;
            if ((sistema.ArrGifts[0] == null) && (sistema.ArrGifts[1] == null) && (sistema.ArrGifts[2] == null))
            {
                rbnObjeto.Checked = true;
                rbnObjeto2.Checked = true;
                rbnObjeto3.Checked = true;
            }

        }

        private void cargarDatos()
        {
            foreach (Item item in sistema.Items)
            {
                lstItems.Items.Add(item);
            }
            if (sistema.ArrGifts[0] != null)
            {
                Gift gift1 = sistema.ArrGifts[0];
                if (gift1.Item != null)
                {
                    rbnItem.Checked=true;
                    txtDescripcion1.Text = gift1.Item.Nombre;
                }
                else
                {
                    rbnObjeto.Checked = true;
                    txtDescripcion1.Text = gift1.Descripcion;
                }

                txtHasta1.Text = gift1.RangoTope.ToString();
                txtDesde1.Text = gift1.RangoBase.ToString();
                txtPrecio1.Text = gift1.Precio.ToString();
            }
            if (sistema.ArrGifts[1] != null)
            {
                Gift gift2 = sistema.ArrGifts[1];
                if (gift2.Item != null)
                {   
                    rbnItem2.Checked=true;
                    txtDescripcion2.Text = gift2.Item.Nombre;
                }
                else
                {
                    rbnObjeto2.Checked = true;
                    txtDescripcion2.Text = gift2.Descripcion;
                }
                txtHasta2.Text = gift2.RangoTope.ToString();
                txtDesde2.Text = gift2.RangoBase.ToString();
                txtPrecio2.Text = gift2.Precio.ToString();
            }

            if (sistema.ArrGifts[2] != null)
            {
                Gift gift3 = sistema.ArrGifts[2];
                if (gift3.Item != null)
                {
                    rbnItem3.Checked = true;
                    txtDescripcion3.Text = gift3.Item.Nombre;
                }
                else
                {
                    rbnObjeto3.Checked = true;
                    txtDescripcion3.Text = gift3.Descripcion;
                }
                txtHasta3.Text = gift3.RangoTope.ToString();
                txtDesde3.Text = gift3.RangoBase.ToString();
                txtPrecio3.Text = gift3.Precio.ToString();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if ((sistema.ArrGifts[0] != null) || (sistema.ArrGifts[1] != null) || (sistema.ArrGifts[2] != null))
                    sistema.modificarRangosGift(txtDesde1.Text, txtHasta1.Text, txtPrecio1.Text, item1, txtDescripcion1.Text, txtDesde2.Text, txtHasta2.Text, txtPrecio2.Text, item2, txtDescripcion2.Text, txtDesde3.Text, txtHasta3.Text, txtPrecio3.Text, item3, txtDescripcion3.Text);
                else
                    sistema.insertarRangosGift(txtDesde1.Text, txtHasta1.Text, txtPrecio1.Text, item1, txtDescripcion1.Text, txtDesde2.Text, txtHasta2.Text, txtPrecio2.Text, item2, txtDescripcion2.Text, txtDesde3.Text, txtHasta3.Text, txtPrecio3.Text, item3, txtDescripcion3.Text);
                lblError.Text = "";
                MessageBox.Show("Rangos guardados con éxito", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                lblError.ForeColor = Color.Red;
                lblError.Text = ex.Message;
            }
        }

        private void btnVolver_Click(object sender, EventArgs e)
        {
            FormMenu menu = new FormMenu();
            menu.Show();
            this.Visible = false;
        }

        private void lstItems_SelectedValueChanged(object sender, EventArgs e)
        {
 
        }

        private void rbnItem_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.RadioButton)(sender)).Checked)
            {
                if (!cargandoForm)
                {
                    selected_item = "item1";
                    lstItems.Enabled = true;
                    txtDescripcion1.ReadOnly = true;
                    groupBox2.Enabled = false;
                    groupBox3.Enabled = false;
                }
            }
            else
            {
                lstItems.Enabled = false;
                txtDescripcion1.ReadOnly = false;
                groupBox2.Enabled = true;
                groupBox3.Enabled = true;
            }
        }

        private void rbnObjeto_CheckedChanged(object sender, EventArgs e)
        {
            item1 = null;
            lstItems.Enabled = false;
            txtDescripcion1.ReadOnly = false;
        }

        private void rbnRango3_CheckedChanged(object sender, EventArgs e)
        {
            txtHasta1.Enabled = false;
            txtDesde1.Enabled = true;
        }

        private void rbnRango_CheckedChanged(object sender, EventArgs e)
        {
            txtDesde1.Enabled = false;
            txtHasta1.Enabled = true;
        }

        private void rbnRango2_CheckedChanged(object sender, EventArgs e)
        {
            txtDesde1.Enabled = true;
            txtHasta1.Enabled = true;
        }

        private void btnItemPick_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;
            groupBox3.Enabled = true;
            try
            {
                Item item = (Item)(lstItems.SelectedItem);
                switch (selected_item)
                {
                    case "item1":
                        {
                            item1 = item;
                            txtDescripcion1.Text = item.ToString();
                            txtDescripcion1.ReadOnly = true;
                            break;
                        }
                    case "item2":
                        {
                            item2 = item;
                            txtDescripcion2.Text = item.ToString();
                            txtDescripcion2.ReadOnly = true;
                            break;
                        }
                    case "item3":
                        {
                            item3 = item;
                            txtDescripcion3.Text = item.ToString();
                            txtDescripcion3.ReadOnly = true;
                            break;
                        }
                }


            }
            catch (Exception ex)
            { }
            lstItems.Enabled = false;
        }

        private void rbnItem3_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.RadioButton)(sender)).Checked)
            {
                if (!cargandoForm)
                {
                    selected_item = "item3";
                    lstItems.Enabled = true;
                    txtDescripcion3.ReadOnly = true;
                    groupBox2.Enabled = false;
                    groupBox1.Enabled = false;
                }
            }
            else
            {
                lstItems.Enabled = false;
                txtDescripcion3.ReadOnly = false;
                groupBox2.Enabled = true;
                groupBox1.Enabled = true;
            }
        }

        private void rbnItem2_CheckedChanged(object sender, EventArgs e)
        {
            if (((System.Windows.Forms.RadioButton)(sender)).Checked)
            {
                if (!cargandoForm)
                {
                    selected_item = "item2";
                    lstItems.Enabled = true;
                    txtDescripcion2.ReadOnly = true;
                    groupBox1.Enabled = false;
                    groupBox3.Enabled = false;
                }
            }
            else
            {
                lstItems.Enabled = false;
                txtDescripcion2.ReadOnly = false;
                groupBox1.Enabled = true;
                groupBox3.Enabled = true;
            }
        }

        private void txtHasta1_Leave(object sender, EventArgs e)
        {
            int hasta1;
            if (Int32.TryParse(((System.Windows.Forms.TextBox)(sender)).Text, out hasta1))
                txtDesde2.Text = (hasta1 + 1).ToString();
        }

        private void txtHasta2_Leave(object sender, EventArgs e)
        {
            int hasta2;
            if (Int32.TryParse(((System.Windows.Forms.TextBox)(sender)).Text, out hasta2))
                txtDesde3.Text = (hasta2 + 1).ToString();
        }

        private void rbnObjeto2_CheckedChanged(object sender, EventArgs e)
        {
            item2 = null;
        }

        private void rbnObjeto3_CheckedChanged(object sender, EventArgs e)
        {
            item3 = null;
        }

        private void FormGift_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        
    }
}
